using System;
using System.Collections.Generic;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using System.Xml;

using Antix;

namespace Antix.Data.Catalogue.Objects
{
    /// <summary>
    /// Database Table Collection
    /// </summary>
    public class DatabaseTableCollection : DatabaseObjectCollection
    {
        #region Constructor

        /// <summary>
        /// Create collection
        /// </summary>
        internal DatabaseTableCollection(Database database)
        {
            Database = database;
        }

        #endregion

        #region Properties

        public Database Database { get; private set; }

        #region Indexers

        /// <summary>
        /// Get by key
        /// </summary>
        /// <param name="schema">schema</param>
        /// <param name="name">name</param>
        /// <returns></returns>
        public DatabaseTable this[string schemaName, string name]
        {
            get { return this[schemaName, name, false]; }
        }

        /// <summary>
        /// Get by key
        /// </summary>
        /// <param name="schema">schema</param>
        /// <param name="name">name</param>
        /// <param name="create">create</param>
        /// <returns></returns>
        public DatabaseTable this[string schemaName, string name, bool create]
        {
            get
            {
                string key = DatabaseTable.GetKey(schemaName, name);
                if (base.Contains(key))
                {
                    return (DatabaseTable)base[key];
                }else if (create) { return this.Add(schemaName, name); }

                return null;
            }
        }

        #endregion

        #endregion

        #region Methods

        #region Contains

        /// <summary>
        /// Collection contains table
        /// </summary>
        /// <param name="schemaName">Schema</param>
        /// <param name="name">Name</param>
        /// <returns>True if found</returns>
        public bool Contains(string schemaName, string name)
        {
            return base.Contains(DatabaseTable.GetKey(schemaName, name));
        }

        #endregion

        #region Add

        /// <summary>
        /// Add new table on default (dbo) schema
        /// </summary>
        /// <param name="name">Table name</param>
        public DatabaseTable Add(string name)
        {
            return this.Add("dbo", name);
        }

        /// <summary>
        /// Add a new table
        /// </summary>
        /// <param name="schemaName">Schema</param>
        /// <param name="name">Name</param>
        public DatabaseTable Add(string schemaName, string name)
        {
            DatabaseTable newItem = new DatabaseTable(this.Database);
            newItem.Schema = this.Database.Schemas[schemaName, true];
            newItem.Name = name;

            base.Add(newItem);

            return newItem;
        }

        /// <summary>
        /// Add Existing item to collection
        /// </summary>
        /// <param name="item"></param>
        internal void Add(DatabaseTable item)
        {
            base.Add(item);
        }

        #endregion

        #endregion
    }
}